package cn.xerllent.quartz.rest;


import cn.hutool.core.bean.BeanUtil;
import cn.xerllent.common.annotation.ForbidSubmit;
import cn.xerllent.common.annotation.Log;
import cn.xerllent.common.entity.vo.ResultData;
import cn.xerllent.common.exception.BadRequestException;
import cn.xerllent.quartz.entity.QuartzJob;
import cn.xerllent.quartz.service.QuartzJobService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.quartz.Job;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

@Slf4j
@RestController
@Api(tags = "系统:定时任务管理")
@RequestMapping("/api/task")
public class QuartzJobController {

    private final QuartzJobService quartzJobService;


    public QuartzJobController(QuartzJobService quartzJobService) {
        this.quartzJobService = quartzJobService;
    }

    @Log("查询定时任务")
    @ApiOperation("查询定时任务")
    @GetMapping
    @PreAuthorize("@el.check('Admin','task:list')")
    public ResponseEntity<Object> getJobs(QuartzJob filter, Pageable pageable) {
        return new ResponseEntity<>(ResultData.data(quartzJobService.queryAll(filter, pageable)), HttpStatus.OK);
    }

    @Log("导出任务数据")
    @ApiOperation("导出任务数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('Admin','task:list')")
    public void download(HttpServletResponse response, QuartzJob filter) throws IOException {
        quartzJobService.download(quartzJobService.queryAll(filter), response);
    }

    @ForbidSubmit
    @Log("新增定时任务")
    @ApiOperation("新增定时任务")
    @PostMapping
    @PreAuthorize("@el.check('Admin','task:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody QuartzJob resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new quartzJob cannot already have an ID");
        }
        quartzJobService.save(resources);
        return new ResponseEntity<>(ResultData.msg("创建成功", 200), HttpStatus.CREATED);
    }

    @ForbidSubmit
    @Log("修改定时任务")
    @ApiOperation("修改定时任务")
    @PutMapping
    @PreAuthorize("@el.check('Admin','task:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody QuartzJob resources) {
        quartzJobService.updateById(resources);
        return new ResponseEntity<>(ResultData.msg("修改成功", 200), HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("更改定时任务状态")
    @ApiOperation("更改定时任务状态")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@el.check('Admin','task:edit')")
    public ResponseEntity<Object> updateEnabled(@PathVariable Integer id) {
        quartzJobService.toggleEnabled(quartzJobService.getOne(new LambdaQueryWrapper<QuartzJob>()
                .eq(QuartzJob::getId, id)));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("执行定时任务")
    @ApiOperation("执行定时任务")
    @PutMapping(value = "/exec/{id}")
    @PreAuthorize("@el.check('Admin','task:edit')")
    public ResponseEntity<Object> execution(@PathVariable Integer id) {
        quartzJobService.execution(quartzJobService.getOne(new LambdaQueryWrapper<QuartzJob>().eq(QuartzJob::getId, id)));
        return new ResponseEntity<>(HttpStatus.OK);
    }
    
    @ForbidSubmit
    @Log("获取定时任务的日志类别type")
    @ApiOperation("获取定时任务的日志类别type")
    @GetMapping(value = "/logtype/{id}")
    @PreAuthorize("@el.check('Admin','task:edit')")
    public ResponseEntity<Object> logtype(@PathVariable Integer id) {
        return new ResponseEntity<>(ResultData.data(quartzJobService.getLogtype(id)),HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("删除定时任务")
    @ApiOperation("删除定时任务")
    @DeleteMapping
    @PreAuthorize("@el.check('Admin','task:del')")
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        quartzJobService.removeByIds(new ArrayList<>(Arrays.asList(ids)));
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
